package cn.com.lyj6851.modules.activity.controller;

import cn.com.lyj6851.logging.aop.AopLog;
import cn.com.lyj6851.modules.activity.domain.YdyStoreCouponIssue;
import cn.com.lyj6851.modules.activity.service.YdyStoreCouponIssueService;
import cn.com.lyj6851.modules.activity.service.dto.YdyStoreCouponIssueQueryCriteria;
import cn.com.lyj6851.modules.aop.ForbidSubmit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
*
*/
@Api(tags = "商城:优惠券发布管理")
@RestController
@RequestMapping("api")
public class StoreCouponIssueController {

    private final YdyStoreCouponIssueService yxStoreCouponIssueService;

    public StoreCouponIssueController(YdyStoreCouponIssueService yxStoreCouponIssueService) {
        this.yxStoreCouponIssueService = yxStoreCouponIssueService;
    }

    @AopLog("查询已发布")
    @ApiOperation(value = "查询已发布")
    @GetMapping(value = "/ydyStoreCouponIssue")
    @PreAuthorize("@per.check('admin','STORECOUPONISSUE_ALL','STORECOUPONISSUE_SELECT')")
    public ResponseEntity getYdyStoreCouponIssues(YdyStoreCouponIssueQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxStoreCouponIssueService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @AopLog("发布")
    @ApiOperation(value = "发布")
    @PostMapping(value = "/ydyStoreCouponIssue")
    @PreAuthorize("@per.check('admin','STORECOUPONISSUE_ALL','STORECOUPONISSUE_CREATE')")
    public ResponseEntity create(@Validated @RequestBody YdyStoreCouponIssue resources){
        if(resources.getTotalCount() > 0) {
            resources.setRemainCount(resources.getTotalCount());
        }
        return new ResponseEntity<>(yxStoreCouponIssueService.save(resources),HttpStatus.CREATED);
    }

    @AopLog("修改状态")
    @ApiOperation(value = "修改状态")
    @PutMapping(value = "/ydyStoreCouponIssue")
    @PreAuthorize("@per.check('admin','STORECOUPONISSUE_ALL','STORECOUPONISSUE_EDIT')")
    public ResponseEntity update(@Validated @RequestBody YdyStoreCouponIssue resources){
        yxStoreCouponIssueService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @ForbidSubmit
    @AopLog("删除")
    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/ydyStoreCouponIssue/{id}")
    @PreAuthorize("@el.check('admin','STORECOUPONISSUE_ALL','STORECOUPONISSUE_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id){
        yxStoreCouponIssueService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
